package com.qf.service;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.utils.Base64Util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class UserServiceImpl implements UserService {
    private UserDao userDao ;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User login(String username, String password) {
        password = Base64Util.encode(password);
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("spring-context.xml");
        UserDao bean = ac.getBean(UserDao.class);
        User user = bean.selectLogin(username, password);
//        User user = userDao.selectLogin(username,password);
//        System.out.println(username);
//        System.out.println(password);
        if (user == null) {
            System.out.println(user);
            throw new RuntimeException("用户密码输入错误");
        }
        if (!user.getPassword().equals(password)){
            throw new RuntimeException("密码错误");
        }
        return user;


    }

    @Override
    public void insert(User user) {

        ApplicationContext ac =
                new ClassPathXmlApplicationContext("spring-context.xml");
        UserDao bean = ac.getBean(UserDao.class);
        bean.insert(user);
    }

    @Override
    public boolean userCheckName(String username) {
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("spring-context.xml");
        UserDao bean = ac.getBean(UserDao.class);
        User user = bean.selectName(username);
        if (user != null){
            return true;
        }
        return false;
    }
}
